using Moq;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class NamedRuleSpecification {
        [Test]
        public void ShouldAddResultOfProductionParseIfSuccessfullyParsed () {
            NamedRule rule = new NamedRule ("a", IdentifierProduction.CreateTerminal ());

            var source = "let";

            RuleParseResult result = rule.Parse (source, 0, new ParseContext ());
            Assert.IsNotNull (result);

            Assert.AreEqual (1, result.Captures.Count);
            result.AssertCaptureIs (0, "a", "let");
        }

        [Test]
        public void ShouldNotAddResultIfNotParsed () {
            NamedRule rule = new NamedRule ("a", IdentifierProduction.CreateTerminal ());

            var source = "9";

            Assert.IsNull (rule.Parse (source, 0, null));
        }

        [Test]
        public void ShouldReturnTrueToHasProduction () {
            Assert.That (((IRule) new NamedRule ("a", new Mock<IProduction> ().Object)).HasProduction);
        }

        [Test]
        public void ProductionShouldBeProductionGiven () {
            var production = new Mock<IProduction> ().Object;
            Assert.That (((IRule) new NamedRule ("a", production)).Production, Is.SameAs (production));
        }
    }
}